package lab10;

public class Encryption {

	private static String encrypt(String message, int[][] key){
		if (message.length()%2!=0) message+="*";
		char[] msg = message.toCharArray();
		message=new String();
		for (int i = 0; i < key.length; i++) {
			char temp = msg[key[i][0]*2];
			msg[key[i][0]*2]=msg[key[i][1]*2+1];
			msg[key[i][1]*2+1]=temp;
		}
//		for (int i = 0; i < msg.length; i++) message+=msg[i];
		for (int i = 0; i < msg.length; i+=2) message+=msg[i];
		for (int i = 1; i < msg.length; i+=2) message+=msg[i];
		return message;
	}
	
	private static String decrypt(String encryptedMessage, int[][] key) {
		char[] msg = encryptedMessage.toCharArray();
		for (int i = key.length-1; i >= 0; i--) {
			char temp = msg[key[i][0]];
			msg[key[i][0]]=msg[key[i][1]+msg.length/2];
			msg[key[i][1]+msg.length/2]=temp;
		}
		String message = new String();
		for (int i = 0; i < msg.length; i++) {
			if (i%2==0) message+=msg[i/2];
			else message+=msg[msg.length/2+i/2];
		}
		return message;
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[][] key = {{4,8},{2,8},{2,7},{4,7},{4,7}};
		String message = "Lots of symbols and shit";
		String encrypted = encrypt(message,key);
		String decrypted = decrypt(encrypted,key);
		System.out.println(message + "\n" + encrypted + "\n" + decrypted);
	}

}
